﻿#region Copyright (c) 2003 - 2008, Andreas Mueller
/////////////////////////////////////////////////////////////////////////////////////////
// 
// Copyright (c) 2003 - 2008, Andreas Mueller.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
//    Andreas Mueller - initial API and implementation
//
// 
// This software is derived from software bearing the following
// restrictions:
// 
// Copyright (c) 1994
// Hewlett-Packard Company
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Hewlett-Packard Company makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// Copyright (c) 1996,1997
// Silicon Graphics Computer Systems, Inc.
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Silicon Graphics makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// (C) Copyright Nicolai M. Josuttis 1999.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// 
/////////////////////////////////////////////////////////////////////////////////////////
#endregion


using System.Collections;
using NStl.Iterators;
using NStl.Iterators.Support;
using NUnit.Framework;
using NStl;
using NStl.Collections.Private;
using System.Collections.Generic;
using NUnit.Framework.SyntaxHelpers;
using NStl.SyntaxHelper;

namespace NStl.NUnit.ContainerFixture.HashContainer
{
    [TestFixture]
    public class HashContainerFixture
    {
        private readonly IUnaryFunction<int, bool> isPrime =
            Functional.PtrFun<int, bool>(delegate(int l)
                                   {
                                       if (l <= 0)
                                           return false;
                                       if(l < 9)
                                       {
                                           if (l == 9) return false;
                                           if (l == 8) return false;
                                           if (l == 7) return true;
                                           if (l == 6) return false;
                                           if (l == 5) return true;
                                           if (l == 4) return false;
                                           if (l == 3) return true;
                                           if (l == 2) return false;
                                           if (l == 1) return true;
                                       }
                                       if (l % 9 == 0) return false;
                                       if (l % 8 == 0) return false;
                                       if (l % 7 == 0) return false;
                                       if (l % 6 == 0) return false;
                                       if (l % 5 == 0) return false;
                                       if (l % 4 == 0) return false;
                                       if (l % 3 == 0) return false;
                                       if (l % 2 == 0) return false;
                                       return true;
                                   });

        [Test]
        public void CheckIsPrimeFunctor()
        {
            Assert.IsTrue(isPrime.Execute(1));
            Assert.IsFalse(isPrime.Execute(2));
            Assert.IsTrue(isPrime.Execute(3));
            Assert.IsFalse(isPrime.Execute(4));
            Assert.IsTrue(isPrime.Execute(5));
            Assert.IsFalse(isPrime.Execute(6));
            Assert.IsTrue(isPrime.Execute(7));
            Assert.IsFalse(isPrime.Execute(8));
            Assert.IsFalse(isPrime.Execute(9));

            Assert.IsTrue(isPrime.Execute(11));
        }
        [Test]
        public void CheckPrimeArray()
        {
            ListIterator<int> found =
                Algorithm.FindIf(HashContainerHelper.first, HashContainerHelper.last, Logical.Not1(isPrime));

            Assert.AreEqual(HashContainerHelper.last, found);
        }

        [Test]
        public void InsertEqualWithoutCollision()
        {
            Assert.AreEqual(expected.Count, hc.Count);

            ICollection actual = NStlUtil.Collection(hc.Begin(), hc.End());
            CollectionAssert.AreEquivalent(expected, actual);
        }

        private HashContainer<string, string> hc;
        private ArrayList expected;
        [SetUp]
        public void SetUp()
        {
            hc = new HashContainer<string, string>(1, Project.Identity<string>());
            expected = new ArrayList();
            for(int i = 0; i < 10; ++i)
            {
                expected.Add(i.ToString());
                hc.InsertUnique(i.ToString());
            }
        }
        [Test]
        public void InsertUniqueWithoutCollision()
        {
            KeyValuePair<HashContainer<string, string>.Iterator, bool> it = hc.InsertUnique("42");
            Assert.AreNotEqual(hc.End(), it.Key);
            Assert.IsTrue(it.Value);

        }
        [Test]
        public void InsertUniqueWithoutCollisionFails()
        {
            KeyValuePair<HashContainer<string, string>.Iterator, bool> it = hc.InsertUnique("1");
            Assert.AreEqual("1", it.Key.Value);
            Assert.IsFalse(it.Value);

        }
        private class CollisionInt
        {
            private readonly int Value;
            internal CollisionInt(int value)
            {
                Value = value;
            }
            public override int GetHashCode()
            {
                return Value.GetHashCode();
            }
        }
        [Test]
        public void InsertEqualWithCollision()
        {
            HashContainer<CollisionInt, CollisionInt> hashContainer = 
                new HashContainer<CollisionInt, CollisionInt>(1, Project.Identity<CollisionInt>());

            ArrayList expectedContent = new ArrayList();


            CollisionInt c1 = new CollisionInt(42);
            hashContainer.InsertEqual(c1);
            expectedContent.Add(c1);

            CollisionInt c2 = new CollisionInt(42);
            HashContainer<CollisionInt, CollisionInt>.Iterator it =
                hashContainer.InsertEqual(c2);
            expectedContent.Add(c2);

            Assert.That(it.Value, Is.SameAs(c2));

            Assert.That(hashContainer.Count, Is.EqualTo(2));
            Assert.That(expectedContent, Is.EquivalentTo(hashContainer));
        }
        [Test]
        public void InsertUniqueWithCollision()
        {
            HashContainer<CollisionInt, CollisionInt> hashContainer = 
                new HashContainer<CollisionInt, CollisionInt>(1, Project.Identity<CollisionInt>());

            ArrayList expectedContent = new ArrayList();


            CollisionInt c1 = new CollisionInt(42);
            hashContainer.InsertUnique(c1);
            expectedContent.Add(c1);

            CollisionInt c2 = new CollisionInt(42);
            KeyValuePair<HashContainer<CollisionInt, CollisionInt>.Iterator, bool> rv =
                hashContainer.InsertUnique(c2);
            expectedContent.Add(c2);

            Assert.That(rv.Value, Is.True);
            Assert.That(rv.Key.Value, Is.SameAs(c2));

            Assert.That(hashContainer.Count, Is.EqualTo(2));
            Assert.That(expectedContent, Is.EquivalentTo(hashContainer));
        }

        [Test]
        public void Capacity()
        {
            Assert.AreEqual(53, hc.Capacity);
        }

        [Test]
        public void InsertEqualAndResize()
        {
            Assert.AreEqual(53, hc.Capacity);

            for (int i = 10; i < 54; ++i)
                hc.InsertEqual(i.ToString());

            Assert.AreEqual(97, hc.Capacity);
        }
        [Test]
        public void Find()
        {
            HashContainer<string, string>.Iterator it =
                hc.Find("5");

            Assert.AreNotEqual(hc.End(), it);
        }
        [Test]
        public void FindFailed()
        {
            HashContainer<string, string>.Iterator it =
                hc.Find("42");

            Assert.AreEqual(hc.End(), it);
        }

        [Test]
        public void EqualRange()
        {
            KeyValuePair<HashContainer<string, string>.Iterator, HashContainer<string, string>.Iterator> eqr =
                hc.EqualRange("5");

            ICollection exp = new string[] { "5"};
            ICollection actual = new ArrayList(NStlUtil.Collection(eqr.Key, eqr.Value));
            CollectionAssert.AreEquivalent(exp, actual);

            Assert.AreEqual("5", eqr.Key.Value);
            Assert.AreEqual(eqr.Key.PreIncrement(), eqr.Value);
        }
        [Test]
        public void EqualRangeWithCollision()
        {
            hc.InsertEqual("5");
            KeyValuePair<HashContainer<string, string>.Iterator, HashContainer<string, string>.Iterator> eqr =
                hc.EqualRange("5");

            ICollection exp = new string[] {"5", "5"};

            CollectionAssert.AreEquivalent(exp, NStlUtil.Collection(eqr.Key, eqr.Value));
        }
        [Test]
        public void EqualRangeFailed()
        {
            KeyValuePair<HashContainer<string, string>.Iterator, HashContainer<string, string>.Iterator> eqr =
                hc.EqualRange("42");

            Assert.AreEqual(hc.End(), eqr.Key);
            Assert.AreEqual(hc.End(), eqr.Value);
        }
        [Test]
        public void FindOrInsertWithInsert()
        {
            string res = 
                hc.FindOrInsert("42");
            Assert.AreEqual("42", res);

            Assert.AreEqual(expected.Count + 1, hc.Count);
        }
        [Test]
        public void FindOrInsertWithoutInsert()
        {
            string res =
                hc.FindOrInsert("5");
            Assert.AreEqual("5", res);

            Assert.AreEqual(expected.Count, hc.Count);
        }
        [Test]
        public void Erase()
        {
            HashContainer<string, string>.Iterator sec = 
                NStlUtil.Advance<string, HashContainer<string, string>.Iterator>(hc.Begin(), 1);

            expected.Remove(sec.Value);
            hc.Erase(sec);

            CollectionAssert.AreEquivalent(expected, hc);
        }

       
        [Test]
        public void EraseFails()
        {
            hc.Erase(hc.End());
            CollectionAssert.AreEquivalent(expected, hc);
        }

        [Test]
        public void EraseRange()
        {
            HashContainer<string, string>.Iterator it1 =
                NStlUtil.Advance<string, HashContainer<string, string>.Iterator>(hc.Begin(), 1);

            HashContainer<string, string>.Iterator it2 =
                NStlUtil.Advance<string, HashContainer<string, string>.Iterator>(hc.Begin(), 5);

            foreach (object curToDelete in NStlUtil.Collection(it1, it2))
                expected.Remove(curToDelete);

            hc.Erase(it1, it2);

            CollectionAssert.AreEquivalent(expected, hc);
        }
        

        [Test]
        public void EraseAll()
        {
            hc.Erase(hc.Begin(), hc.End());

            CollectionAssert.IsEmpty(hc);
        }
        [Test]
        public void EraseWithFailsForeighnIterator()
        {
            HashContainer<string, string>.Iterator i =
                new HashContainer<string, string>(0, Project.Identity<string>()).Begin();
            hc.Erase(i);
            CollectionAssert.AreEquivalent(expected, hc);
        }

        [Test]
        public void EraseKey()
        {
            Assert.AreEqual(1, hc.Erase("5"));
            expected.Remove("5");
            CollectionAssert.AreEquivalent(expected, hc);
        }
        [Test]
        public void EraseKeyWithCollision()
        {
            hc.InsertEqual("5");
            Assert.AreEqual(2, hc.Erase("5"));
            expected.Remove("5");
            CollectionAssert.AreEquivalent(expected, hc);
        }
    }
}
